home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / util / cdity / ModeProSrc.lha / Daemon / BestMode.c < prev    next >
C/C++ Source or Header  |  1999-02-20  |  6KB  |  260 lines

  1.  
  2. //#define DEBUG
  3. #include <debug.h>
  4.  
  5. #include <proto/exec.h>
  6. #include <proto/graphics.h>
  7. #include <proto/utility.h>
  8.   
  9. #include <math.h>
  10. #include <stdlib.h>
  11.  
  12. #include <graphics/modeid.h>
  13. #include <extras/macros/exec.h>
  14. #include <tagitemmacros.h>
  15.  
  16. #include "mp.h"
  17.  
  18. LONG MyAbs(LONG L);
  19.  
  20. ULONG ValidDisplayID(ULONG DispID);
  21. ULONG ValidMonitorID(ULONG DispID);
  22.  
  23. extern struct MPSem *MPSem;
  24.  
  25. LONG MyAbs(LONG L)
  26. {
  27.   if(L<0)
  28.     return(-L);
  29.   return(L);
  30. }
  31.  
  32.  
  33.   
  34. ULONG MyBestModeID(Tag Tags, ... )
  35. {
  36.   struct TagItem *taglist,*tag,*tstate;
  37.   ULONG id,bestid,bestscore,score,disq; //(disqualified)
  38.   ULONG data,
  39.         sourceid,
  40.         monitorid=MONITOR_ID_MASK,
  41.         depth,
  42.         dipfmusthave,
  43.         dipfmustnothave;
  44.   LONG  nwidth,
  45.         nheight,
  46.         dwidth,
  47.         dheight;
  48.   struct DisplayInfo    dis;      
  49.   struct DimensionInfo  dim;
  50.   
  51.   depth=  dipfmusthave= dipfmustnothave=  nwidth=  nheight=  dwidth=  dheight=0;  
  52.   
  53.   taglist=(APTR)&Tags;
  54.  
  55. DEBUG_CODE(DKP("MyBestModeID()\n"););
  56.  
  57.   ProcessTagList(taglist,tag,tstate)
  58.   {
  59.     data=tag->ti_Data;
  60.     switch(tag->ti_Tag)
  61.     {
  62.       case BIDTAG_SourceID:
  63.         sourceid=data;
  64. DEBUG_CODE(DKP("BIDTAG_SourceID 0x%08lx\n",data););
  65.         if(GetDisplayInfoData(0,(APTR)&dis,sizeof(dis),DTAG_DISP,sourceid))
  66.         {
  67.           dipfmusthave=(dis.PropertyFlags & (DIPF_IS_DUALPF | DIPF_IS_PF2PRI | DIPF_IS_HAM | DIPF_IS_EXTRAHALFBRITE));
  68.         }
  69.         
  70.         if(GetDisplayInfoData(0,(APTR)&dim,sizeof(dim),DTAG_DIMS,sourceid))
  71.         {
  72.           dwidth  =nwidth  =(dim.Nominal.MaxX-dim.Nominal.MinX);
  73.           dheight =nheight =(dim.Nominal.MaxY-dim.Nominal.MinY);
  74.         }   
  75.         break;
  76.       case BIDTAG_MonitorID:
  77. DEBUG_CODE(DKP("BIDTAG_MonitorID 0x%08lx\n",data););
  78.         monitorid=ValidMonitorID(data);//                                                              (4.62.10)
  79.         break;
  80.       case BIDTAG_Depth:
  81. DEBUG_CODE(DKP("BIDTAG_Depth %ld\n",data););
  82.         depth=min(data,32);
  83.         break;
  84.       case BIDTAG_DIPFMustHave:
  85. DEBUG_CODE(DKP("BIDTAG_DIPFMustHave 0x%08lx\n",data););
  86.         dipfmusthave=data;
  87.         break;
  88.       case BIDTAG_DIPFMustNotHave:
  89. DEBUG_CODE(DKP("BIDTAG_DIPFMustNotHave 0x%08lx\n",data););
  90.         dipfmustnothave=data;
  91.         break;
  92.       case BIDTAG_DesiredWidth:
  93. DEBUG_CODE(DKP("BIDTAG_DesiredWidth %ld\n",data););
  94.         dwidth=data;
  95.         break;
  96.       case BIDTAG_DesiredHeight:
  97. DEBUG_CODE(DKP("BIDTAG_DesiredHeight %ld\n",data););
  98.         dheight=data;
  99.         break;
  100.       case BIDTAG_NominalWidth:
  101. DEBUG_CODE(DKP("BIDTAG_NominalWidth %ld\n",data););
  102.         nwidth=data;
  103.         break;
  104.       case BIDTAG_NominalHeight:
  105. DEBUG_CODE(DKP("BIDTAG_NominalHeight %ld\n",data););
  106.         nheight=data;     
  107.         break;
  108.     }
  109.   }
  110.   
  111.   if(!nwidth)
  112.     nwidth=dwidth;
  113.   
  114.   if(!nheight)
  115.     nheight=dheight;
  116.  
  117. /*  if(!dwidth)//                                                                                      (4.62.13)
  118.     dwidth=nwidth;//                                                                                   (4.62.13)
  119.   //                                                                                                   (4.62.13)
  120.   if(!dheight)//                                                                                       (4.62.13)
  121.     dheight=nheight;//                                                                                 (4.62.13)
  122. *///                                                                                                   (4.62.13)
  123.  
  124.   bestscore=0;
  125.   bestid=
  126.   id=INVALID_ID;
  127.   
  128.   while(INVALID_ID != (id=NextDisplayInfo(id)))
  129.   {
  130.     disq=0;
  131.     score=0;
  132.  
  133. DEBUG_CODE(DKP("\n");DKP("  Comparing 0x%08lx\n",id););
  134.  
  135.     if(GetDisplayInfoData(0,(APTR)&dis,sizeof(dis),DTAG_DISP,id))
  136.     {
  137. DEBUG_CODE(DKP("    PropertyFlags 0x%08lx   MustHave 0x%08lx   MustNotHave 0x%08lx\n",id,dipfmusthave,dipfmustnothave););      
  138.       /* make sure it has dipfmusthave flags */
  139.       if(dipfmusthave != (dipfmusthave & dis.PropertyFlags))
  140.       {
  141. DEBUG_CODE(DKP("      Missing Flags\n"););
  142.         disq=1;
  143.       }
  144.       
  145.       /* make sure it doesn't have dipfmustnothave flags */
  146.       if(dipfmustnothave & dis.PropertyFlags)
  147.       {
  148. DEBUG_CODE(DKP("      Has unwanted Flags\n"););
  149.         disq=1;
  150.       }
  151.     }
  152.  
  153.     if(monitorid!=MONITOR_ID_MASK)
  154.     {
  155.       if(monitorid != (id & MONITOR_ID_MASK))
  156.       {
  157. DEBUG_CODE(DKP("    Invalid Monitor\n"););
  158.         disq=1;
  159.       }
  160.     }
  161.     
  162.     if(GetDisplayInfoData(0,(APTR)&dim,sizeof(dim),DTAG_DIMS,id))
  163.     {
  164.       LONG width,height,ws,hs,ds;
  165.       
  166.       width   =dim.Nominal.MaxX - dim.Nominal.MinX + 1;
  167.       height  =dim.Nominal.MaxY - dim.Nominal.MinY + 1;
  168.       
  169.       ws=65536-MyAbs(nwidth  - width);
  170.       hs=65536-MyAbs(nheight - height);
  171.       ds=0;
  172.  
  173.       if(depth>0)
  174.       {
  175.         if(depth>dim.MaxDepth)
  176.         {
  177. DEBUG_CODE(DKP("    Invalid Depth\n");); 
  178.           disq=1;
  179.         }
  180.         else
  181.         {
  182.           if(depth<9) // register
  183.           {
  184.             if(dim.MaxDepth<9) // weed out true color screens
  185.             {
  186.               ds=65536;
  187.             }
  188.           }
  189.           else // truecolor
  190.           {
  191.             if(dim.MaxDepth>8) // weed out register screens
  192.             {
  193.               ds=65536-(abs(dim.MaxDepth-depth)*2048);
  194.             }
  195.           }
  196.         }
  197.       }
  198.       
  199.       score+=ws+hs+ds;
  200.       
  201. DEBUG_CODE(DKP("    Width %ld (%ld)   Height %ld (%ld)  Depth %ld (%ld) (Score)\n",width,ws,height,hs,dim.MaxDepth,ds););      
  202.     }
  203.     
  204.     if(!disq)
  205.     {
  206. DEBUG_CODE(DKP("    -- Score:%ld\n",score););
  207.       
  208.       if(score>bestscore)
  209.       {
  210.         bestid=id;
  211.         bestscore=score;
  212.       }
  213.     }
  214.     else
  215.     {
  216. DEBUG_CODE(DKP("    Disqualified\n"););
  217.     }
  218.   }
  219.   
  220.   DEBUG_CODE(DKP("  BestMode 0x%08lx\n",bestid););
  221.   
  222.   return(bestid);  
  223. }
  224.  
  225.  
  226.  
  227. ULONG ValidMonitorID(ULONG DispID)//                                                                   (4.62.9)
  228. {
  229.   ULONG id;
  230.   
  231.   DispID &= MONITOR_ID_MASK;
  232.   id=NextDisplayInfo(INVALID_ID);
  233.  
  234.   while(id!=INVALID_ID)
  235.   {
  236.     if((id & MONITOR_ID_MASK) == DispID)
  237.       return(DispID);
  238.     id=NextDisplayInfo(id);
  239.   }
  240.   return(MONITOR_ID_MASK);
  241. }
  242.  
  243. /*
  244. ULONG ValidDisplayID(ULONG DispID)
  245. {
  246.   ULONG previd=(INVALID_ID & MONITOR_ID_MASK),
  247.         id,mid;
  248.   
  249.   id=NextDisplayInfo(INVALID_ID);
  250.  
  251.   while(id!=INVALID_ID)
  252.   {
  253.     if(id == DispID)
  254.       return(DispID);
  255.       
  256.     id=NextDisplayInfo(id);
  257.   }
  258.   
  259.   return(INVALID_ID);
  260. }*/